package com.edu.model.constant;

import java.util.regex.Pattern;

public interface EventConstant {

    interface LoopConstant {
        String ADD_INDEX_SYMBOL = "+ index";
        String ADD_LOOP_INDEX_SYMBOL = "+ loopIndex";
        String INIT_NODE_STORAGE_ARRAY = "!<INIT_NODE_STORAGE_ARRAY>!";
    }

    enum ConstantMapping {
        REQUIRED(0, "必填项"),
        UN_REQUIRED(1, "非必填项");

        ConstantMapping(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        private int value;

        private String desc;

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

    interface NodeTypeConstant {
        /**
         * http事件
         */
        int HTTP = 101;
        /**
         * 条件判断
         */
        int IF = 201;
        /**
         * 路径选择
         */
        int SWITCH = 202;
        /**
         * 数值计算
         */
        int CALC = 203;
        /**
         * NodeJs脚本
         */
        int NODEJS = 204;

        int LOOP = 206;
        int LOOP_END = 207;
        /**
         * API服务调用
         */
        int API = 301;
        /**
         * 键值对操作
         */
        int REDIS = 503;
        /**
         * HTTP响应
         */
        int RESPONSE = 701;
    }

    /**
     * 来源
     */
    interface SourceConstant {
        /**
         * 固定值
         */
        int VAL = 0;
        /**
         * 来自节点
         */
        int FRO = 1;
    }

    /**
     * sourceType(0-数值型（默认） 1-布尔型（0-false，1-true） 2-字符串 3-时间型（2020-06-12 16:31:00）)
     * targetType（同sourceType）
     */
    class ParamType {
        public static final int NUMBER = 0;
        public static final int BOOLEAN = 1;
        public static final int STRING = 2;
        public static final int DATE = 3;
        public static final int ARRAY = 4;
        public static final int STRUCT = 5;

        public static <T> T wrapper(Integer sourceType, String value) {
            if (sourceType == null) {
                return null;
            }
            switch (sourceType) {
                case BOOLEAN: {
                    if ("0".equals(value)) {
                        return (T) Boolean.FALSE;
                    } else {
                        return (T) Boolean.TRUE;
                    }
                }
                case DATE: {
                }
                case NUMBER: {
                }
                case STRING: {
                    return (T) ("'" + value + "'");
                }
                case ARRAY: {
                }
                case STRUCT: {
                    return (T) value;
                }
            }
            return null;
        }
    }

    interface Patterns {
        String ACTION_START = "^(/+)";
        String ACTION_END = "(/+)$";
        Pattern NODE_JS_ARGS_PATTERN = Pattern.compile("<[0-9]+@[A-Za-z0-9_]*>");
        Pattern httpRVarPattern = Pattern.compile("\"\\{\\{([0-9]*[1-9][0-9]*@\\w*}})\"");
    }

    String ID_SEPARATOR_VAL = "@";

    String FUNC_PREFIX = "func";

    String FUNC_NAME_SPLICE_SYMBOL = "_";

    /**
     * 对比
     */
    String[] Operates = {"==", "!=", "<", "<=", ">", ">=", "== null", "!= null"};


    /**
     * 操作符 +- * /
     */
    String[] OperateSign = {"+", "-", "*", "/", "Math.max.apply", "Math.min.apply"};

    class DbConstant {
        public static final int QUERY = 0;
        public static final int INSERT = 1;
        public static final int DELETE = 2;
        public static final int UPDATE = 3;

        public static final String LINK_SEPARATOR = "@";
        public static final String SPLIT_SEPARATOR = ",";
        public static final String DB_FILE_NAME_PREFIX_SIMPLE = "DATA_SOURCE_CONFIG_JSON";
        public static final String DB_OBJECT_NAME = "DATA_SOURCE_CONFIG_JSON";
        public static final String REDIS_SYMBOL_PREFIX = "Redis_";
        public static final String HTTP_SYMBOL_PREFIX = "Http_";
        public static final String REDIS_SYMBOL_PREFIX_SIMPLE = "Redis";
        public static final String KAFKA_SYMBOL_PREFIX_SIMPLE = "Kafka";
    }

    interface ContentTypeConstant {
        String JSON = "application/json";
        String FORM = "application/x-www-form-urlencoded";
    }

    interface HttpMethod {
        String POST = "POST";
        String GET = "GET";
    }

    interface EventType {
        int HTTP = 0;
    }

    interface DataSourceParseType {
        int INNER = 0;
        int DEFAULT_REDIS = 1;
    }

    interface EventState {
        /**
         * 服务部署状态变化
         * -2：编译异常，旧版本仍然在运行
         * -1：编译异常
         * 0：开发中，显示发布（不可点）
         * 1：编译完成，显示发布
         * 2：运行中，显示停止
         * 3：停止，显示启动
         * 4：运行中（有改动），显示运行中（有新版本）
         * <p>
         * 0-->1：保存
         * 1-->2：部署
         * 2-->3：停止
         * 3-->1：保存
         * 2-->4：保存
         * 4-->2：部署
         * <p>
         */
        int COMPILE_FAILED_AT_RUNNING_STATE = -2;
        int COMPILE_FAILED_STATE = -1;
        int NEW_STATE = 0;
        int COMPILED_STATE = 1;
        int RUNNING_STATE = 2;
        int STOP_STATE = 3;
        int RECOMPILED_AT_RUNNING_STATE = 4;
    }

    enum OperateType implements EventState {
        COMPILE_FAILED_AT_RUNNING(COMPILE_FAILED_AT_RUNNING_STATE, "运行中（编译异常）", false),
        COMPILE_FAILED(COMPILE_FAILED_STATE, "编译异常", false),
        NEW(NEW_STATE, "待编辑", false),
        COMPILED(COMPILED_STATE, "部署", true),
        RUNNING(RUNNING_STATE, "停止", true),
        STOP(STOP_STATE, "启动", true),
        RECOMPILED_AT_RUNNING(RECOMPILED_AT_RUNNING_STATE, "运行中（有新版本）", true);

        private int state;
        private String show;
        private boolean isOperable;

        OperateType(int state, String show, boolean isOperable) {
            this.state = state;
            this.show = show;
            this.isOperable = isOperable;
        }

        public int getState() {
            return state;
        }

        public String getShow() {
            return show;
        }

        public boolean isOperable() {
            return isOperable;
        }
    }

}
